package com.umakr.ax.wechat.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/**
 * @author wangmingdong
 */
public class AddSha1 {
    public static String sha1(String inStr) {
        MessageDigest md = null;
        String outStr = null;
        try {
            //选择SHA-1，也可以选择MD5
            md = MessageDigest.getInstance("SHA-1");
            //返回的是byet[]，要转化为String存储比较方便
            byte[] digest = md.digest(inStr.getBytes());
            outStr = bytetoString(digest);
        }
        catch (NoSuchAlgorithmException nsae) {
            nsae.printStackTrace();
        }
        return outStr;
    }


    public static String bytetoString(byte[] digest) {
        String str = "";
        String tempStr = "";
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < digest.length; i++) {
            tempStr = (Integer.toHexString(digest[i] & 0xff));
            if (tempStr.length() == 1) {
                stringBuilder.append("0").append(tempStr);
            }else {
                stringBuilder.append(tempStr);
            }
        }
        str = stringBuilder.toString();
        return str.toLowerCase();
    }
}
